package com.firewolf.main;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.firewolf.dto.User;

public class MainDriver {

	private SessionFactory factory;
	
	private Session session;
	
	private Transaction transaction;
	
	
	@Test
	public void testType(){
		openSession();
		User u = new User("liuxing", false, new Date());
		session.save(u);
		
		closeSession();
	}
	
	
	@Before
	public void init() {
		Configuration cfg = new Configuration().configure();

		ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(cfg.getProperties())
				.buildServiceRegistry();
		factory = cfg.buildSessionFactory(registry);
	}

	@After
	public void destory() {
		factory.close();
	}
	
	private void openSession(){
		session = factory.openSession();
		transaction = session.beginTransaction();
	}
	
	private void closeSession(){
		transaction.commit();
		session.close();
	}

}
